package practise.alogrithm.hundredInterviewQuestions.secondTime;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by angela on 6/8/2014.
 */
public class PrintArrayInClockwise {
    public List<Integer> print(int[][] array){
        if(array.length==0 || array[0].length==0){
            return new ArrayList<>();
        }
        List<Integer> result= new ArrayList<>();
        int startX=0, startY=0, endX=array[0].length-1,endY=array.length-1;
        while(true){
            for(int index =startX;index<=endX;index++){
                result.add(array[startY][index]);
            }
            if(++startY>endY)break;
            for(int index =startY;index<=endY;index++){
                result.add(array[index][endX]);
            }
            if(--endX<startX)break;
            for(int index =endX;index>=startX;index--){
                result.add(array[endY][index]);
            }
            if(--endY<startY)break;
            for(int index =endY;index>=startY;index--){
                result.add(array[index][startX]);
            }
            if(++startX>endX)break;
        }
        return result;
    }

    public static void main(String[] args){
        System.out.print("result:"+ new PrintArrayInClockwise().print(new int[][]{{1,2,3},{4,5,6},{7,8,9}}));
    }
}
